package com.g9.blog.controller;

import com.g9.blog.common.Response;
import com.g9.blog.common.Result;
import com.g9.blog.doto.ArticleDoto;
import com.g9.blog.entity.Article;
import com.g9.blog.entity.Articleimg;
import com.g9.blog.service.impl.ArticleServiceImpl;
import com.g9.blog.service.impl.MyUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Auther: Graycat
 * @Date: 2020/9/11 10:13
 * Create by lwj.
 */

@RestController
@Transactional
public class ArticleController {

    @Autowired
    private ArticleServiceImpl articleService;
    @Autowired
    private MyUserServiceImpl userService;

    //  上传图片
    @PostMapping("/article/upload")
    public Result uploadArticleImg( MultipartFile file, int articleId ){
        String path = userService.uploadImg(file);
        userService.saveImgPathToSql(path,articleId);
        return Response.makeOKRsp("上传成功");
    }

    // 创建文章 有id就修改，文章跟内容根据传进来得id判断
    @PostMapping("/article")
    public Result postArticle( @RequestBody ArticleDoto articleDoto ){
        return articleService.createOrUpdataArtile(articleDoto);
    }

    // 删除 -- 删文章-》删评论-》删articleimg ？？
    @DeleteMapping("/article/{articleId}")
    public Result deleteArticle( @PathVariable int articleId ){
        articleService.deleteById(articleId);
        return  Response.makeOKRsp("删除成功.");
    }

    // 查找所有文章
    @GetMapping("/article")
    public Result findAllArticle(  ){
        List<Article> articleList = articleService.findAllArticle();
        return Response.makeOKRsp(articleList);
    }

    // 根据用户名查找该用户的所有文章
    @GetMapping("/article/user/{userid}")
    public Result findAllArticleByUserid( @PathVariable int userid ){
        List<Article> articleList = articleService.findAllArticleByUserId(userid);
        return Response.makeOKRsp(articleList);
    }

    // 根据文章id查找文章信息
    @GetMapping("/article/{articleId}")
    public Result findAllArticleByArticleid( @PathVariable int articleId ){
        List<Article> articleList = articleService.findAllArticleById(articleId);
        return Response.makeOKRsp(articleList);
    }

    // 根据文章id返回该文章的所有图片
    @GetMapping("/articleimg/{articleId}")
    public Result findArticleImgByArticleId( @PathVariable int articleId ){
        List<Articleimg> imgList = articleService.findAllImgByArticleId(articleId);
        return Response.makeOKRsp(imgList);
    }

}
